<?php
header('Content-Type:text/html;charset=utf-8');
session_start();
require_once './config/sys_config.php';
require_once MB_CONF_DIR  . '/user_config.php';
require_once MB_COMM_DIR  . '/util.class.php';
require_once MB_APP_DIR   . '/global.class.php';
require_once MB_MODEL_DIR . '/oauth.class.php';
require_once MB_MODEL_DIR . '/opent.class.php';
require_once MB_MODEL_DIR . '/api_client.class.php';

//设置logger
$logger = new MBLog($mbConfig["log_path"], "reg", false);
MBGlobal::setLogger($logger);

if($_POST['test_reg']){
	$_SESSION['username'] = $_POST['username'];
	$_SESSION['password'] = $_POST['password'];
	//检查用户名密码合法性
	if(true){
		//todo
		$checkOk = true;
	}
	//检查通过后进行Oauth认证
	if($checkOk){
		$o = new MBOpenTOAuth(MB_AKEY, MB_SKEY);
		$keys = $o->getRequestToken(MBUtil::getCurUrl());//这里填上你的回调URL
		$aurl = $o->getAuthorizeURL($keys['oauth_token'], false, '');
		$_SESSION['keys'] = $keys;
		header("Location: $aurl");
	}
}

//获取ACCESS_TOKEN
if($_REQUEST['oauth_verifier']){
	$o = new MBOpenTOAuth(MB_AKEY, MB_SKEY, $_SESSION['keys']['oauth_token'], $_SESSION['keys']['oauth_token_secret']);
	$last_key = $o->getAccessToken($_REQUEST['oauth_verifier']);
	//将获取的三个值保存到session即获得了iweibo的访问授权
	$_SESSION["access_token"] = $last_key['oauth_token'];
	$_SESSION["access_token_secret"] = $last_key['oauth_token_secret'];
	$_SESSION['name'] = $last_key["name"];
	var_dump($_SESSION);
	//剩下的就是注册的业务逻辑了
	//比如一套ACCESS_TOKEN是否与站点用户一一对应之类的
	//todo
	//最后把获取的ACCESS_TOKEN的三个值保存给对应的站点用户
	//todo
	echo "<a href='index.php'>可以iweibo访问了</a>";
}

//登录就是通过用户名、密码把存到数据库里的ACCESS_TOKEN的三个值从数据库取出来保存到session中
if($_POST['test_login']){
	echo "登录就是通过用户名、密码把存到数据库里的ACCESS_TOKEN的三个值从数据库取出来保存到session中";
	//todo
	//$_SESSION["access_token"] = $db['oauth_token'];
	//$_SESSION["access_token_secret"] = $db['oauth_token_secret'];
	//$_SESSION['name'] = $db["name"];
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>测试注册绑定</title>
	</head>
	<body>
		<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
			<label>注册</label>
			用户名：<input type="text" name="username" id="username" />
			密&nbsp;&nbsp;码：<input type="password" name="password" id="password" />
			<input type="hidden" name="test_reg" value="1">
			<input type="submit" name="button" id="button" value="提交" />
		</form>
		
		<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
			<label>登录</label>
			用户名：<input type="text" name="username" id="username" />
			密&nbsp;&nbsp;码：<input type="password" name="password" id="password" />
			<input type="hidden" name="test_login" value="1">
			<input type="submit" name="button" id="button" value="提交" />
		</form>
	</body>
</html>